package tom.client.stock;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import tom.client.DataForm;
import tom.client.Library;
import tom.client.LookupDialog;
import tom.shared.Product;
import tom.shared.Stock;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class FormProduct extends DataForm {
	private final TextBox txtName;
	private final TextBox txtPurPrice;

	private final TextBox txtUnit;
	private final Label lbUnit;
	private final TextBox txtDiscount;
	private final Label lbDiscount;
	private final TextBox txtSellPrice;
	private final Label lbSellPrice;
	private final TextBox txtMinPrice;
	private final Label lbMinPrice;
	private final Label lbPurVat;
	private final TextBox txtPurVat;
	private final CheckBox chkStockItem;
	private final CheckBox chkSellItem;
	private final CheckBox chkBuyItem;
	private final CheckBox chkActive;
	private final TextArea txtDescript;
	private final TextBox txtDc1;
	private final TextBox txtDc2;
	private final TextBox txtDc3;
	private final TextBox txtDc4;
	private final TextBox txtDc5;
	private final TextBox txtBarCode;
	private final ListBox listBox;
	private List<Double> vat;
	private final Label totBalance;
	private final Button btnUnit = new Button("?");
	private final Button btnStkGroup = new Button("?");
	private final Button btnCostLedger = new Button("?");
	private final Button btnSellLedger = new Button("?");

	AsyncCallback<List<Double>> vatCallback = new AsyncCallback<List<Double>>() {

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Double> result) {
			if (result != null) {
				vat = result;
			}

		}
	};

	AsyncCallback<Product> proCallback = new AsyncCallback<Product>() {

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());
		}

		@Override
		public void onSuccess(Product result) {
			record = result;
			setFields();
		}

	};

	public FormProduct() {
		lbCode.setSize("23px", "30px");

		Label lblNewLabel = new Label("ชื่อสินค้า");
		add(lblNewLabel, 23, 126);
		lblNewLabel.setSize("93px", "30px");

		txtName = new TextBox();
		add(txtName, 122, 126);
		txtName.setSize("227px", "18px");

		txtPurPrice = new TextBox();
		txtPurPrice.setReadOnly(true);
		add(txtPurPrice, 122, 187);
		txtPurPrice.setSize("106px", "16px");

		Label lblNewLabel_1 = new Label("ส่วนลด (%)");
		add(lblNewLabel_1, 23, 157);
		lblNewLabel_1.setSize("93px", "28px");

		txtUnit = new TextBox();
		add(txtUnit, 122, 337);
		txtUnit.setSize("106px", "16px");

		lbUnit = new Label("ราคาซื้อล่าสุด");

		add(lbUnit, 23, 187);
		lbUnit.setSize("93px", "28px");

		txtDiscount = new TextBox();
		add(txtDiscount, 122, 157);
		txtDiscount.setSize("45px", "16px");
		txtDiscount.setAlignment(TextAlignment.RIGHT);

		lbDiscount = new Label("หน่วยนับ");
		add(lbDiscount, 23, 337);
		lbDiscount.setSize("93px", "28px");

		txtSellPrice = new TextBox();
		add(txtSellPrice, 122, 247);
		txtSellPrice.setSize("106px", "16px");

		lbSellPrice = new Label("ราคาขาย");
		add(lbSellPrice, 23, 247);
		lbSellPrice.setSize("93px", "28px");

		txtMinPrice = new TextBox();
		add(txtMinPrice, 122, 307);
		txtMinPrice.setSize("106px", "16px");

		lbMinPrice = new Label("ราคาต่ำสุด");
		add(lbMinPrice, 23, 307);
		lbMinPrice.setSize("93px", "28px");

		lbPurVat = new Label("VATCODE ซื้อ");
		add(lbPurVat, 23, 217);
		lbPurVat.setSize("93px", "28px");

		txtPurVat = new TextBox();
		add(txtPurVat, 122, 217);
		txtPurVat.setSize("106px", "16px");

		txtSellVat = new TextBox();
		add(txtSellVat, 122, 277);
		txtSellVat.setSize("106px", "16px");

		lbSellVat = new Label("VATCODE ขาย");
		add(lbSellVat, 23, 277);
		lbSellVat.setSize("93px", "28px");

		lbIncomeAcc = new Label("Sell Ledger");
		add(lbIncomeAcc, 374, 95);
		lbIncomeAcc.setSize("89px", "21px");

		txtIncomeAcc = new TextBox();
		add(txtIncomeAcc, 476, 95);
		txtIncomeAcc.setSize("106px", "16px");

		lbCostAcc = new Label("Cost Ledger");
		add(lbCostAcc, 374, 127);
		lbCostAcc.setSize("89px", "21px");

		txtCostAcc = new TextBox();
		add(txtCostAcc, 476, 127);
		txtCostAcc.setSize("106px", "16px");

		chkStockItem = new CheckBox("Stock Item");
		add(chkStockItem, 372, 307);

		chkSellItem = new CheckBox("Sell Item");
		add(chkSellItem, 372, 332);

		chkBuyItem = new CheckBox("Buy Item");
		add(chkBuyItem, 372, 357);

		chkActive = new CheckBox("Active");
		add(chkActive, 309, 76);

		txtDescript = new TextArea();
		add(txtDescript, 374, 218);
		txtDescript.setSize("206px", "47px");

		Label lbDescript = new Label("หมายเหตุ");
		add(lbDescript, 374, 190);
		lbDescript.setSize("54px", "21px");

		txtProGrp = new TextBox();
		add(txtProGrp, 122, 371);
		txtProGrp.setSize("106px", "16px");

		lblStkgroup = new Label("StkGroup");
		add(lblStkgroup, 20, 371);
		lblStkgroup.setSize("93px", "28px");

		txtDc1 = new TextBox();
		add(txtDc1, 209, 157);
		txtDc1.setSize("45px", "16px");
		txtDc1.setAlignment(TextAlignment.RIGHT);

		Label label_1 = new Label("1");
		add(label_1, 199, 162);
		label_1.setSize("23px", "18px");

		Label label_2 = new Label("2");
		add(label_2, 286, 162);
		label_2.setSize("23px", "18px");

		txtDc2 = new TextBox();
		add(txtDc2, 296, 157);
		txtDc2.setSize("45px", "16px");
		txtDc2.setAlignment(TextAlignment.RIGHT);

		Label label_3 = new Label("3");
		add(label_3, 374, 162);
		label_3.setSize("23px", "18px");

		txtDc3 = new TextBox();
		add(txtDc3, 384, 157);
		txtDc3.setSize("45px", "16px");
		txtDc3.setAlignment(TextAlignment.RIGHT);

		Label label_4 = new Label("4");
		add(label_4, 461, 162);
		label_4.setSize("23px", "18px");

		txtDc4 = new TextBox();
		add(txtDc4, 471, 157);
		txtDc4.setSize("45px", "16px");
		txtDc4.setAlignment(TextAlignment.RIGHT);

		txtDc5 = new TextBox();
		add(txtDc5, 559, 157);
		txtDc5.setSize("45px", "16px");
		txtDc5.setAlignment(TextAlignment.RIGHT);

		label_5 = new Label("5");
		add(label_5, 549, 162);
		label_5.setSize("23px", "18px");

		listBox = new ListBox();
		add(listBox, 476, 307);
		listBox.setSize("110px", "69px");
		listBox.setVisibleItemCount(5);

		totBalance = new Label("0");
		totBalance.setDirection(Direction.RTL);
		add(totBalance, 529, 382);
		totBalance.setSize("61px", "16px");

		Label lblNewLabel_2 = new Label("Balance");
		add(lblNewLabel_2, 476, 382);

		btnUnit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				LookupDialog.show("เลือก Unit", "unitCode", txtUnit);
			}
		});
		add(btnUnit, 239, 337);
		btnUnit.setSize("30px", "30px");

		btnStkGroup.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				LookupDialog.show("เลือก Stock Group", "proGrp", txtProGrp);
			}
		});
		add(btnStkGroup, 239, 371);
		btnStkGroup.setSize("30px", "30px");

		btnCostLedger.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				LookupDialog.show("เลือก Ledger", "accCode", txtCostAcc);
			}
		});
		add(btnCostLedger, 598, 126);
		btnCostLedger.setSize("30px", "30px");

		btnSellLedger.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				LookupDialog.show("เลือก Ledger", "accCode", txtIncomeAcc);
			}
		});
		add(btnSellLedger, 598, 93);
		btnSellLedger.setSize("30px", "30px");
		
		txtBarCode = new TextBox();
		add(txtBarCode, 122, 405);
		txtBarCode.setSize("106px", "18px");
		
		Label lblNewLabel_3 = new Label("Barcode");
		add(lblNewLabel_3, 20, 411);

		this.setFieldsHandler();

		Library.getStkService().selectVAT(vatCallback);
	}

	private static FormProduct panel;

	public static FormProduct getPanel() {
		if (panel == null)
			panel = GWT.create(FormProduct.class);
		return panel;
	}

	private Product record;

	@Override
	protected void search() {
		LookupDialog.show("เลือกสินค้า", "proCode", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getStkService().selectProduct(txtCode.getText(), dataCallback);
	}

	@Override
	protected void update() {
		getFields();
		Library.getStkService().updateProduct(record, delCallback);
	}

	@Override
	protected void insert() {
		getFields();
		Library.getStkService().insertProduct(record, delCallback);
	}

	@Override
	protected void delete() {
		if (Window.confirm("ต้องการลบ " + record.name + " หรือไม่ ?")) {
			getFields();
			Library.getStkService().deleteProduct(txtCode.getText(),
					delCallback);
		}
	}

	@Override
	protected void getFields() {
		record.proCode = txtCode.getText();
		record.name = txtName.getText();
		record.descript = txtDescript.getText();
		record.proGrp = txtProGrp.getText();
		record.unit = txtUnit.getText();
		if (!txtDiscount.getText().equals(""))
			record.discount = Double.parseDouble(txtDiscount.getText());
		if (!txtSellPrice.getText().equals(""))
			record.sellPrice = Double.parseDouble(txtSellPrice.getText());
		if (!txtPurPrice.getText().equals(""))
			record.purPrice = Double.parseDouble(txtPurPrice.getText());
		if (!txtMinPrice.getText().equals(""))
			record.minPrice = Double.parseDouble(txtMinPrice.getText());
		record.purVAT = txtPurVat.getText();
		record.sellVAT = txtSellVat.getText();
		// record.supplier =txtSupplier.getText();
		record.incomeAcc = txtIncomeAcc.getText();
		record.costAcc = txtCostAcc.getText();
		record.stockItem = chkStockItem.getValue();
		record.sellItem = chkSellItem.getValue();
		record.buyItem = chkBuyItem.getValue();
		record.active = chkActive.getValue();
		if (!txtDc1.getText().equals(""))
			record.dc1 = Double.parseDouble(txtDc1.getText());
		if (!txtDc2.getText().equals(""))
			record.dc2 = Double.parseDouble(txtDc2.getText());
		if (!txtDc3.getText().equals(""))
			record.dc3 = Double.parseDouble(txtDc3.getText());
		if (!txtDc4.getText().equals(""))
			record.dc4 = Double.parseDouble(txtDc4.getText());
		if (!txtDc5.getText().equals(""))
			record.dc5 = Double.parseDouble(txtDc5.getText());
		record.barcode = txtBarCode.getText();

	}

	@Override
	protected String getReportFile() {
		return null;
	}

	@Override
	protected Map<String, Object> genReportParams() {
		return null;
	}

	AsyncCallback<Product> dataCallback = new AsyncCallback<Product>() {

		@Override
		public void onSuccess(Product result) {
			record = result;
			setFields();
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};
	private TextBox txtSellVat;
	private Label lbSellVat;
	private Label lbIncomeAcc;
	private TextBox txtIncomeAcc;
	private Label lbCostAcc;
	private TextBox txtCostAcc;
	private TextBox txtProGrp;
	private Label lblStkgroup;
	private Label label_5;

	protected void setFields() {
		super.setFields();
		if (record == null) {
			record = new Product();
			super.setMode(3);
		}
		txtName.setText(record.name);
		txtDescript.setText(record.descript);
		txtProGrp.setText(record.proGrp);
		txtUnit.setText(record.unit);
		txtDiscount.setText(record.discount.toString());
		txtSellPrice.setText(record.sellPrice.toString());
		txtPurPrice.setText(record.purPrice.toString());
		txtMinPrice.setText(record.minPrice.toString());
		txtPurVat.setText(record.purVAT.toString());
		txtSellVat.setText(record.sellVAT.toString());
		// record.supplier =txtSupplier.getText();
		txtIncomeAcc.setText(record.incomeAcc);
		txtCostAcc.setText(record.costAcc);
		chkStockItem.setValue(record.stockItem);
		chkSellItem.setValue(record.sellItem);
		chkBuyItem.setValue(record.buyItem);
		chkActive.setValue(record.active);
		txtDc1.setText(record.dc1.toString());
		txtDc2.setText(record.dc2.toString());
		txtDc3.setText(record.dc3.toString());
		txtDc4.setText(record.dc4.toString());
		txtDc5.setText(record.dc5.toString());
		txtBarCode.setText(record.barcode);
		listBox.clear();
		for (Stock s : record.stocks) {
			listBox.addItem(s.stkCode + " : " + s.balance);
		}
		totBalance.setText(String.valueOf(record.getBalance()));

		btnUnit.setEnabled(true);
		btnStkGroup.setEnabled(true);
		btnCostLedger.setEnabled(true);
		btnSellLedger.setEnabled(true);
	}

	@Override
	protected void clearFields() {
		super.clearFields();

		txtName.setText(null);
		txtDescript.setText(null);
		txtProGrp.setText(null);
		txtUnit.setText(null);
		txtDiscount.setText(null);
		txtSellPrice.setText(null);
		txtPurPrice.setText(null);
		txtMinPrice.setText(null);
		txtPurVat.setText(null);
		txtSellVat.setText(null);
		// record.supplier =txtSupplier.getText();
		txtIncomeAcc.setText(null);
		txtCostAcc.setText(null);
		chkStockItem.setValue(null);
		chkSellItem.setValue(null);
		chkBuyItem.setValue(null);
		chkActive.setValue(null);
		txtDc1.setText(null);
		txtDc2.setText(null);
		txtDc3.setText(null);
		txtDc4.setText(null);
		txtDc5.setText(null);
		listBox.clear();
		totBalance.setText(null);
//		btnUnit.setEnabled(false);
//		btnStkGroup.setEnabled(false);
//		btnCostLedger.setEnabled(false);
//		btnSellLedger.setEnabled(false);
	}
}
